package com.banquetmanagementsystem.config;


import com.banquetmanagementsystem.interceptor.JwtAccessTokenInterceptor;
import com.banquetmanagementsystem.interceptor.JwtTokenInterceptor;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author yzj
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Resource(name = "jwtTokenInterceptor")
    private JwtTokenInterceptor jwtTokenInterceptor;
    @Resource(name = "jwtAccessTokenInterceptor")
    private JwtAccessTokenInterceptor jwtAccessTokenInterceptor;
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        WebMvcConfigurer.super.addCorsMappings(registry);
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowedMethods("GET","HEAD","POST","PUT","DELE")
                .allowCredentials(true)
                .maxAge(3600)
                .allowedHeaders("*");
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(jwtTokenInterceptor)
                .addPathPatterns("/newToken");
        registry.addInterceptor(jwtAccessTokenInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/login","/newToken","/CaptchaImage");
    }
}